﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Common
{
    public static class MathExtensions
    {
        public static Double Cap(Double value, Double? minimum, Double? maximum)
        {
            if (minimum != null)
                value = Math.Max(value, minimum.Value);
            if (maximum != null)
                value = Math.Min(value, maximum.Value);
            return value;
        }

        public static int Cap(int value, int? minimum, int? maximum)
        {
            if (minimum != null)
                value = Math.Max(value, minimum.Value);
            if (maximum != null)
                value = Math.Min(value, maximum.Value);
            return value;
        }

        public static Decimal Cap(Decimal value, Decimal? minimum, Decimal? maximum)
        {
            if (minimum != null)
                value = Math.Max(value, minimum.Value);
            if (maximum != null)
                value = Math.Min(value, maximum.Value);
            return value;
        }

        public static Decimal Round(Decimal number, Decimal nearest)
        {
            return Math.Round((number + 0.000000000000001M) / nearest) * nearest;
        }

        public static Double Round(Double value, Double nearest)
        {
            return Math.Round((value + 0.000000000000001) / nearest) * nearest;
        }

        public static Double Floor(Double value, Double nearest)
        {
            return Math.Floor(value / nearest) * nearest;
        }

        public static Double Ceiling(Double value, Double nearest)
        {
            return Math.Ceiling(value / nearest) * nearest;
        }
    }
}
